//
//  YKRequestExchangeViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKRequestPerformanceExchangeViewController.h"
#import "YKInputView.h"
#import "YKPopupView.h"
#import "YKInputMultilineView.h"
#import "YKProcessTrackingViewController.h"
#import "YKPerformanceExchangeDetailModel.h"
#import "YKEditPerformanceExchangeViewController.h"
#import "YKApproveBottomView.h"
#import "YKTurnOverViewController.h"
#import "YKTurnOverUserModel.h"
@interface YKRequestPerformanceExchangeViewController ()
@property (nonatomic, strong) YKPerformanceExchangeDetailModel *mYKPerformanceExchangeDetailModel;
@end

@implementation YKRequestPerformanceExchangeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"申请兑换";
    
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"achievementPointsExchange/searchAchievementPointsExchangeListById.htm" parameters:@{@"id":self.m_id} success:^(id responseObject) {
        self.mYKPerformanceExchangeDetailModel = [YKPerformanceExchangeDetailModel yy_modelWithJSON:responseObject[@"achievementPointsExchange"]];
        [self tf_showStateView:kTFViewStateNone];
        [self updateViews];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
    
    // Do any additional setup after loading the view.
//    [self updateViews];
    
}
- (void)updateViews {
    YKInputView *requestNoView = [[YKInputView alloc]initWithTitle:@"申请单号" input:self.mYKPerformanceExchangeDetailModel.APPLY_CODE];
    [self.view addSubview:requestNoView];
    
    YKInputView *requestPersonView = [[YKInputView alloc]initWithTitle:@"申请人" input:self.mYKPerformanceExchangeDetailModel.APPLY_PERSON];
    [self.view addSubview:requestPersonView];
    
    YKInputView *requestDateView = [[YKInputView alloc]initWithTitle:@"申请时间" input:self.mYKPerformanceExchangeDetailModel.APPLY_DATE];
    [self.view addSubview:requestDateView];
    
    YKInputView *scoreView = [[YKInputView alloc]initWithTitle:@"积分兑换" input:self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT];
    [self.view addSubview:scoreView];
    
    YKInputView *scoreLeftView = [[YKInputView alloc]initWithTitle:@"积分余额" input:self.mYKPerformanceExchangeDetailModel.ACHIEVEMENT_INTEGRAL];
    [self.view addSubview:scoreLeftView];
    
    YKInputMultilineView *exchangeContentView = [[YKInputMultilineView alloc]initWithTitle:@"兑换内容" input:self.mYKPerformanceExchangeDetailModel.EXCHANGE_CONTENT];
    [self.view addSubview:exchangeContentView];
    
    [self.view.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(13);
        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
    }];
    [self.view.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@8 leadSpace:11];
    for (YKInputView *view in self.view.subviews) {
        view.editable = NO;
    }
    
    UIButton* processTrackButton = [[UIButton alloc]init];
    [self.view addSubview:processTrackButton];
    UIButton *editButton = [[UIButton alloc]init];
    [self.view addSubview:editButton];
    UIButton *submitButton = [[UIButton alloc]init];
    [self.view addSubview:submitButton];
    [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [submitButton setTitle:@"提交审批" forState:UIControlStateNormal];
    [processTrackButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [editButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [submitButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [editButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];
    editButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
    [editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[editButton,processTrackButton,submitButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[editButton,processTrackButton,submitButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    
    if (self.approveStatus) {
        [YKApproveBottomView addSelfToView:self ID:self.m_id  approveStatus:self.approveStatus];
    }
}
- (void)edit:(id)sender {
    YKEditPerformanceExchangeViewController *vc = [[YKEditPerformanceExchangeViewController alloc]init];
    vc.mYKPerformanceExchangeDetailModel = self.mYKPerformanceExchangeDetailModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)processTrack:(id)sender {
    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
    vc.m_id = self.m_id;
    [YKPopupView showPopupViewWithViewController:vc];
}
- (void)submit:(id)sender {
    NSString *id_p = self.mYKPerformanceExchangeDetailModel.ID;
    NSString *exchange_point = self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT;
    NSString *original_exchange_point = self.mYKPerformanceExchangeDetailModel.EXCHANGE_POINT;
    NSString *achievement_integral = self.mYKPerformanceExchangeDetailModel.ACHIEVEMENT_INTEGRAL;
    NSString *exchange_content = self.mYKPerformanceExchangeDetailModel.EXCHANGE_CONTENT;
    NSString *apply_person = self.mYKPerformanceExchangeDetailModel.APPLY_PERSON;
    
    [TFHTTPMANAER POST:@"personalCentr/doAddApplyExchangeIntegral.htm" parameters:@{@"id":id_p, @"exchange_point":exchange_point, @"original_exchange_point":original_exchange_point, @"achievement_integral":achievement_integral, @"exchange_content":exchange_content, @"apply_person":apply_person,@"subState":@"1",@"apply_code":self.mYKPerformanceExchangeDetailModel.APPLY_CODE} HUD:@"编辑中..." success:^(id responseObject) {
        YKTurnOverViewController *vc = [YKTurnOverViewController new];
        vc.m_id = self.m_id;
        vc.type = YKTurnOverTypeApprove;
        NSArray *user = [NSArray yy_modelArrayWithClass:[YKTurnOverUserModel class] json:[responseObject valueForKeyPath:@"userList.mapSel"]];
        vc.users = user;
        vc.nextTask = [NSArray yy_modelArrayWithClass:[YKTurnOverUserTaskModel class] json:[responseObject valueForKeyPath:@"nextTask"]].firstObject;
        vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
            [TFHTTPMANAER POST:@"workExamine/completeWorkExamine.htm" parameters:@{@"mark":text,@"businessId":businessId,@"nextId":nextId} HUD:@"转审中..." success:^(id responseObject) {
                [self showHint:@"提交审批成功"];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(id responseObject, NSError *error) {
                [self showHint:@"提交审批失败, 请点击重试"];
            }];
        };
        
        [YKPopupView showPopupViewWithViewController:vc];
    } failure:^(id responseObject, NSError *error) {
        
    }];
}


@end
